layui.use(['jquery', 'layer','form','table'], function(){ 
    var $ = layui.$ //重点处
    ,layer = layui.layer;
    var form = layui.form,
    layedit = layui.layedit;
    var table = layui.table;

    var tableIns = table.render({
        elem: '#test'
            ,cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
            ,data:[
                ]
            ,cols: [[
                    {field: 'id', title: '字段', width:80, sort: true, fixed: 'left'}
                ]
            ]
    });

    $('#queryBut').click(function(){
        var data = $("#inputSql").val();
        $.ajax({
            type: "POST",
            url: 'http://127.0.0.1/test/api',
            data: data,
            success: function(rsp,s){
                console.log(rsp);
                let newtable = parseTable(testData1);
                console.log(newtable)
                table.reload('test', newtable, false)
            }
        });
    });
});

function parseColsProperties(datas){
    let set =new Set();

    for(let item in datas){
        let arrayItem = Object.getOwnPropertyNames(datas[item]);
        arrayItem.forEach((name) =>{set.add(name)})
    }
    return set;
}

function parseCols(set){
    let arrayTitle = new Array();
    set.forEach((v, i, s) => {
        let item = {
            field : v,
            title : v,
            sort: true
        }
        arrayTitle.push(item);
    });
    let arrayTitle2 = new Array();
    arrayTitle2.push(arrayTitle);
    return arrayTitle2;
}

function parseTable(data){
    let option = {
        elem: '#test'
        ,cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
        ,data:data
        ,cols: parseCols(parseColsProperties(data))
    }
    return option;
}